package org.budo.phone.location.service.baidu;

import org.budo.phone.location.service.api.AbstractBudoPhoneLocation;
import org.budo.phone.location.service.api.BudoPhoneLocation;
import org.budo.phone.location.service.api.BudoPhoneLocationService;
import org.budo.support.http.client.Request;
import org.budo.support.http.client.Response;
import org.budo.support.lang.util.StringUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import lombok.extern.slf4j.Slf4j;

/**
 * @author limingwei
 */
@Slf4j
public class BaiduPhoneLocationService implements BudoPhoneLocationService {
    private static final String API_URL = "http://mobsec-dianhua.baidu.com/dianhua_api/open/location"; // 这个接口已

    // https://m.baidu.com/s?word=13785227813
    @Override
    public BudoPhoneLocation seekPhoneLocation(String phone) {
        try {
            return this.seek(phone);
        } catch (Throwable e) {
            log.error("#25 phone=" + phone + ", this=" + this + ", e=" + e, e);
            return null;
        }
    }

    private BudoPhoneLocation seek(String phone) {
        Request request = new Request();
        request.setUrl(API_URL);
        request.setParameter("tel", phone);

        Response response = request.execute();
        String responseBody = response.getBody();
        if (response.getStatus() > 200 || StringUtil.isEmpty(responseBody)) {
            log.error("#25 request=" + request + ", response=" + response);
            return null;
        }

        return this.responseToLocation(phone, responseBody);
    }

    private BudoPhoneLocation responseToLocation(String phone, String responseBody) {
        if (StringUtil.isNullOrEmpty(responseBody)) {
            log.error("#50 responseBody is null, responseBody=" + responseBody + ", phone=" + phone);
            return null;
        }

        JSONObject jsonObject = JSON.parseObject(responseBody);
        // {"response":{"075532914508":{"detail":{"area":[{"city":"深圳","district":["宝安"]}],"province":"广东","type":"domestic"},"location":"广东深圳"}},"responseHeader":{"status":200,"time":1555464822214,"version":"1.1.0"}}

        if (null == jsonObject) {
            log.error("#53 jsonObject is null, jsonObject=" + jsonObject + ", phone=" + phone);
            return null;
        }

        JSONObject response_1 = jsonObject.getJSONObject("response");
        if (null == response_1) {
            log.error("#38 response_1 is null, jsonObject=" + jsonObject + ", phone=" + phone);
            return null;
        }

        JSONObject phoneNoJsonObject = response_1.getJSONObject(phone);
        if (null == phoneNoJsonObject) {
            log.info("#44 phoneNoJsonObject is null, jsonObject=" + jsonObject + ", phone=" + phone);
            return null;
        }

        JSONObject detailJson = phoneNoJsonObject.getJSONObject("detail");
        if (null == detailJson) {
            log.error("#49 detailJson is null, jsonObject=" + jsonObject + ", phone=" + phone);
            return null;
        }

        String serviceProvider = detailJson.getString("operator");
        String province = detailJson.getString("province");

        JSONArray cityArray = detailJson.getJSONArray("area");
        JSONObject cityJson = cityArray.getJSONObject(0);
        String city = cityJson.getString("city");

        // 传入固话时 没有返回运营商
        if (StringUtil.isEmpty(province) || StringUtil.isEmpty(city)) {
            log.error("#74 return null, responseBody=" + responseBody);
            return null;
        }

        return new AbstractBudoPhoneLocation(province, city, serviceProvider);
    }
}